package com.ymttest.database.sqlwapper;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ymt.core.tool.Logger;
import com.ymt.database.SqlManager;
import com.ymttest.database.model.YmtActivity;
import com.ymttest.utils.MapUtil;

/************************************************************************************
 * @File name : YmtUserWapper.java
 * @Author : zhouyi
 * @Date : 2015年4月2日
 * @Copyright : 洋码头
 ************************************************************************************/
public class YmtActivityWapper {
	SqlManager manage;

	public YmtActivityWapper() {
		manage = new SqlManager("intpro");
	}

	/**
	 * 创建一个今天到明天有效的活动
	 * 
	 * @param isellerid
	 *            发布活动的商户sellerid
	 * @return insert成功数
	 * @throws InterruptedException
	 */
	public int insertOneActivity(int isellerid) throws InterruptedException {
		Calendar calendar = Calendar.getInstance();
		YmtActivity record = new YmtActivity();
		record.setIactivitytemplateid(10);
		record.setSname("自动化测试新人活动特惠");
		record.setDaddtime(calendar.getTime());
		record.setDbegintime(calendar.getTime());
		calendar.add(Calendar.DAY_OF_MONTH, 1);
		record.setDendtime(calendar.getTime());
		record.setDapplydeadline(calendar.getTime());
		record.setIaction(1);
		record.setStitleprefix("自动化测试");
		record.setIsellerid(isellerid);
		record.setFdefaultfeerate(new BigDecimal(0.00));
		record.setIappliedspulimit(1);
		record.setIadminid(0);
		record.setIappliedsellertype(0);
		record.setIstocklimit(0);
		record.setIlimitnum(1);
		record.setBuseuserlevelprice(false);
		record.setIuserlevels(0);
		int i = manage.insert("com.ymttest.database.mapping.YmtActivityMapper.insert", record);
		manage.commit();
		// manage.close();
		return i;
	}

	/**
	 * 获取最后一条活动记录
	 * 
	 * @return 最后一条活动记录 YmtActivity
	 * @throws InterruptedException
	 */
	public YmtActivity getLastOneRecord(int isellerid) throws InterruptedException {

		List<YmtActivity> ya = manage.getSelectResults("com.ymttest.database.mapping.YmtActivityMapper.selectLastOne",
				isellerid);
		// manage.close();
		return ya.get(0);
	}
	
	
	public List<YmtActivity> selectactivityid() throws InterruptedException {

		List<YmtActivity> ya = manage.getSelectResults("com.ymttest.database.mapping.YmtActivityMapper.selectactivityid",
				null);
		// manage.close();
		return ya;
	}

	
	public YmtActivity getRecordByActivityId(int iActivityId) throws InterruptedException {

		List<YmtActivity> ya = manage.getSelectResults("com.ymttest.database.mapping.YmtActivityMapper.selectByPrimaryKey",
				iActivityId);
		// manage.close();
		return ya.get(0);
	}
	
	
	
	public int updateActivityLimit(int activityId,int limitType ,int limitNum){
		int i = manage.update("com.ymttest.database.mapping.YmtActivityMapper.updateActivityLimit", 
					MapUtil.hashMap("iActivityId", activityId,"LimitNumType",limitType,"iLimitNum",limitNum));
		manage.commit();
		return i;
	}
	
	
	

//	public List<YmtActivity> searchActivity(SellerActivityBean bean) throws InterruptedException {
//
//		List<YmtActivity> ya = manage.getSelectResults("com.ymttest.database.mapping.YmtActivityMapper.searchActivity",
//				bean);
//		// manage.close();
//		return ya;
//	}
	
	
	/**
	 * 关闭数据库连接
	 */
	public void close() {
		manage.close();
	}

	@Override
	protected void finalize() throws Throwable {
		this.close();
		manage = null;
		super.finalize();
	}

	public static void main(String args[]) {
		System.out.println(new YmtActivityWapper().updateActivityLimit(333332, 1, 33));
	}
}
